package com.sondon.mayi.fragment;

import java.util.ArrayList;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;
import org.apache.http.Header;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.loopj.android.http.TextHttpResponseHandler;
import com.sondon.mayi.activity.CartActivity_;
import com.sondon.mayi.activity.ChooseRegionActivity_;
import com.sondon.mayi.activity.GoodsListActivity_;
import com.sondon.mayi.activity.NoticeActivity_;
import com.sondon.mayi.configs.URLConfigs;
import com.sondon.mayi.entity.Cate;
import com.sondon.mayi.entity.ID_DATA;
import com.sondon.mayi.entity.MyInfoEntity;
import com.sondon.mayi.entity.ShopADEntity;
import com.sondon.mayi.events.ShopInitEvent;
import com.sondon.mayi.fruit.R;
import com.sondon.mayi.ui.MyMultipleTopBar;
import com.sondon.mayi.ui.TopBarClickListener;
import com.sondon.mayi.ui.TopBarTitleClickListener;
import com.sondon.mayi.util.AsyncHttpUtil;
import com.sondon.mayi.util.GsonUtil;
import com.sondon.mayi.util.ImageLoaderUtil;
import com.sondon.mayi.util.LogUtil;
import com.sondon.mayi.util.ToastUtils;

import de.greenrobot.event.EventBus;

/**
 * 
 * @Project : Fruit
 * @Package : com.sondon.mayi.fragment
 * @Class : ShopFragmentPage
 * @Author : 蔡文锋
 * @DateTime：2015年6月15日 下午9:58:17
 * @Blog：http://blog.csdn.net/caiwenfeng_for_23
 * @Description : { 水果页 }
 * 
 */
@EFragment(R.layout.shop)
public class ShopFragmentPage extends BaseFragment {
	@ViewById
	MyMultipleTopBar topBar;// 标题
	@ViewById
	EditText search_context_edit;// 输入查询内容
	@ViewById
	ImageView search_butt;// 查询按钮
	@ViewById
	ImageView img_ad;// 广告
	// @ViewById
	// LinearLayout classify_a;// 分类a--进口水果
	// @ViewById
	// LinearLayout classify_b;// 分类b--国产水果
	// @ViewById
	// LinearLayout classify_c;// 分类c--套餐礼盒
	// @ViewById
	// LinearLayout classify_d;// 分类d--热带专区
	@ViewById
	ImageView classify_a_img;// 分类a--进口水果
	@ViewById
	ImageView classify_b_img;// 分类b--国产水果
	@ViewById
	ImageView classify_c_img;// 分类c--套餐礼盒
	@ViewById
	ImageView classify_d_img;// 分类d--热带专区

	@ViewById
	ImageView recommend_a;// 推荐a
	@ViewById
	ImageView recommend_b;// 推荐b
	@ViewById
	ImageView recommend_c;// 推荐c

	// 各水果模块名字
	public String img_ad_name, classify_a_name, classify_b_name,
			classify_c_name, classify_d_name, recommend_a_name,
			recommend_b_name, recommend_c_name;
	
	//eventbus事件
	public void onEventMainThread(ShopInitEvent event) {
		LogUtil.d("eventbus  shop", "ShopInitEvent ...");

		this.img_ad_name = event.getImg_ad_name();
		
		this.classify_a_name = event.getClassify_a_name();
		this.classify_b_name=event.getClassify_b_name();
		this.classify_c_name=event.getClassify_c_name();
		this.classify_d_name=event.getClassify_d_name();
		
		this.recommend_a_name=event.getRecommend_a_name();
		this.recommend_b_name=event.getRecommend_b_name();
		this.recommend_c_name=event.getRecommend_c_name();
	}

	// 初始化View
	@AfterViews()
	public void initView() {
		// topbar的监听事件
		topBar.setTopBarClickListener(new TopBarClickListener() {

			@Override
			public void rightBtnClick() {
				// 购物车
				openActivity_Login(CartActivity_.class);
			}

			@Override
			public void leftBtnClick() {
				// 动态消息
				openActivity(NoticeActivity_.class);
			}
		});

		// topbar title点击监听事件
		topBar.setTopBarTitleClickListener(new TopBarTitleClickListener() {

			@Override
			public void titleClick() {
				// 选择地区
				openActivity(ChooseRegionActivity_.class);
			}
		});

		// 发送数据初始化请求
		AsyncHttpUtil.get(URLConfigs.ShopAD, null, ShopADResponseHandler);
	}
	
	// 点击事件
	@Click({ R.id.search_butt, R.id.img_ad, R.id.classify_a_img,
			R.id.classify_b_img, R.id.classify_c_img, R.id.classify_d_img,
			R.id.recommend_a, R.id.recommend_b, R.id.recommend_c })
	public void onClick(View v) {
		Bundle bundle=new Bundle();
		switch (v.getId()) {
		case R.id.search_context_edit://搜索输入框
			
			break;
		case R.id.search_butt:// 搜索
			String search_key=search_context_edit.getText().toString().trim();
			myprefs.goodslist_title().put(search_key);
			bundle.putString("param_key", "search[keywords]");
			bundle.putString("param_value", search_key);
			openActivity(GoodsListActivity_.class,bundle);
			break;
		case R.id.img_ad:// 广告
			myprefs.goodslist_title().put(this.img_ad_name);
			bundle.putString("param_key", "search[commend_id]");
			bundle.putString("param_value", String.valueOf(img_ad.getTag()));
			openActivity(GoodsListActivity_.class,bundle);
			break;
		case R.id.classify_a_img:// 分类abcd
			myprefs.goodslist_title().put(this.classify_a_name);
			bundle.putString("param_key", "search[category_id]");
			bundle.putString("param_value", String.valueOf(classify_a_img.getTag()));
			openActivity(GoodsListActivity_.class,bundle);
			break;
		case R.id.classify_b_img:
			myprefs.goodslist_title().put(this.classify_b_name);
			bundle.putString("param_key", "search[category_id]");
			bundle.putString("param_value", String.valueOf(classify_b_img.getTag()));
			openActivity(GoodsListActivity_.class,bundle);
			break;
		case R.id.classify_c_img:
			myprefs.goodslist_title().put(this.classify_c_name);
			bundle.putString("param_key", "search[category_id]");
			bundle.putString("param_value", String.valueOf(classify_c_img.getTag()));
			openActivity(GoodsListActivity_.class,bundle);
			break;
		case R.id.classify_d_img:
			myprefs.goodslist_title().put(this.classify_d_name);
			bundle.putString("param_key", "search[category_id]");
			bundle.putString("param_value", String.valueOf(classify_d_img.getTag()));
			openActivity(GoodsListActivity_.class,bundle);
			break;
		case R.id.recommend_a:// 推荐abc
			myprefs.goodslist_title().put(this.recommend_a_name);
			bundle.putString("param_key", "search[commend_id]");
			bundle.putString("param_value", String.valueOf(recommend_a.getTag()));
			openActivity(GoodsListActivity_.class,bundle);
			break;
		case R.id.recommend_b:
			myprefs.goodslist_title().put(this.recommend_b_name);
			bundle.putString("param_key", "search[commend_id]");
			bundle.putString("param_value", String.valueOf(recommend_b.getTag()));
			openActivity(GoodsListActivity_.class,bundle);
			break;
		case R.id.recommend_c:
			myprefs.goodslist_title().put(this.recommend_c_name);
			bundle.putString("param_key", "search[commend_id]");
			bundle.putString("param_value", String.valueOf(recommend_c.getTag()));
			openActivity(GoodsListActivity_.class,bundle);
			break;
		default:
			break;
		}
	}
	
	@Override
	public void onResume() {
		super.onResume();
		LogUtil.e(TAG, "ShopFragmentPage  onResume....");
		String region_str=myprefs.region().get();
		if(region_str!=null&&!TextUtils.isEmpty(region_str)){
			String[] regions=myprefs.region().get().split("#");
			// 设置区域
			topBar.setTopBarTitleText(getResources().getString(R.string.app_name)
					+ " "+regions[5]);
		}else{
			// 设置区域
			topBar.setTopBarTitleText(getResources().getString(R.string.app_name)
					+ " ");
		}
		
//		getMyInfo();  //查询个人信息  暂时没用
		
		//设置消息
		String news_code=myprefs.news_code().get();
		if("0".equals(news_code)){
			topBar.setNoticeNews(true);
		}else{
			topBar.setNoticeNews(false);
		}
	}
	
	//获取个人信息
	public void getMyInfo(){
		AsyncHttpUtil.get(URLConfigs.MyInfo_ukey+myprefs.Ukey().get(), null, new TextHttpResponseHandler() {
			
			@Override
			public void onSuccess(int status, Header[] arg1, String json) {
				if(200==status){
				LogUtil.e(TAG, "获取个人信息  json ："+json);
				MyInfoEntity entity=GsonUtil.GetFromJson(json, MyInfoEntity.class);
				if(entity.isStatus()){
					// 设置区域
					topBar.setTopBarTitleText(getResources().getString(R.string.app_name)
							+ " "+entity.getInfo().getCity_name());
				}else{
					ToastUtils.show(getActivity(), entity.getMsg());
					}
				}
			}
			
			@Override
			public void onFailure(int arg0, Header[] arg1, String arg2, Throwable arg3) {
				ToastUtils.show(getActivity(), R.string.network_unavailable);
			}
		});
	}
	
	// 绑定用户请求响应监听
	TextHttpResponseHandler BingUserResponseHandler = new TextHttpResponseHandler() {
		@Override
		public void onSuccess(int statusCode, Header[] arg1, String json) {
			if (statusCode == 200) {
				LogUtil.e(TAG, "BingUmengUser  json :" + json);
				// ToastUtils.show(getActivity(), "绑定用户成功");
			}
		}

		@Override
		public void onFailure(int arg0, Header[] arg1, String arg2,
				Throwable arg3) {
			ToastUtils.show(getActivity(), R.string.login_fail);
		}
	};
	
	// 首页信息请求响应监听
	TextHttpResponseHandler ShopADResponseHandler = new TextHttpResponseHandler() {
		@Override
		public void onSuccess(int statusCode, Header[] arg1, String json) {
			if (statusCode == 200) {
				// LogUtil.e(TAG, "ShopAD  json :" + json);
				ShopADEntity shopad = GsonUtil.GetFromJson(json,
						ShopADEntity.class);
				if (shopad.isStatus()) {// 请求数据成功
				// LogUtil.e(TAG,shopad.getList().getCate().get(0).getImg());
					// 顶部广告
					ImageLoaderUtil.SetImgView( shopad.getList().top.get(0).getContent(),img_ad);
					img_ad.setTag(shopad.getList().top.get(0).getCommend_id());
					
					// 分类adbcd
					ArrayList<Cate> cates= shopad.getList().getCate();
					ImageLoaderUtil.SetImgView( cates.get(0).getImg(),classify_a_img);
					classify_a_img.setTag(cates.get(0).getId());
					
					ImageLoaderUtil.SetImgView( cates.get(1).getImg(),classify_b_img);
					classify_b_img.setTag(cates.get(1).getId());
					
					ImageLoaderUtil.SetImgView( cates.get(2).getImg(),classify_c_img);
					classify_c_img.setTag(cates.get(2).getId());
					
					ImageLoaderUtil.SetImgView( cates.get(3).getImg(),classify_d_img);
					classify_d_img.setTag(cates.get(3).getId());

					// 推荐abc
					ArrayList<ID_DATA> bottoms= shopad.getList().getBottom();
					ImageLoaderUtil.SetImgView( bottoms.get(0).getContent(), recommend_a);
					recommend_a.setTag(bottoms.get(0).getCommend_id());
					ImageLoaderUtil.SetImgView( bottoms.get(1).getContent(), recommend_b);
					recommend_b.setTag(bottoms.get(1).getCommend_id());
					ImageLoaderUtil.SetImgView( bottoms.get(2).getContent(), recommend_c);
					recommend_c.setTag(bottoms.get(2).getCommend_id());
					
					//发送event
					EventBus.getDefault().post(new ShopInitEvent(
							shopad.getList().top.get(0).getName(), 
							cates.get(0).getName(),
							cates.get(1).getName(),
							cates.get(2).getName(),
							cates.get(3).getName(), 
							bottoms.get(0).getName(), 
							bottoms.get(1).getName(), 
							bottoms.get(2).getName()));
					
					//清除数据
					cates.clear();
					bottoms.clear();
				} else {
					ToastUtils
							.show(getActivity(), R.string.network_unavailable);
				}
			}
		}

		@Override
		public void onFailure(int arg0, Header[] arg1, String arg2,
				Throwable arg3) {
			ToastUtils.show(getActivity(), R.string.login_fail);
		}
	};

}
